package mk.company.fin.service;

import java.net.URL;
import java.util.Locale;

import mk.company.fin.AppConstants;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

/**
 * Service for manipulating with the messages.
 * 
 * @author vlado.gjorgjiev
 */
@Service
public class MessageService {

	@Autowired
	private MessageSource messageSource;
	
	@Autowired
	private LocaleService localeService;
	
	
	/* TEXT MESSAGES */
	
	public String getMessage(String messageCode) {
		return messageSource.getMessage(messageCode, null, localeService.getCurrentLocale());
	}
	
	public String getMessage(String messageCode, String defaultMessage) {
		return messageSource.getMessage(messageCode, null, defaultMessage, localeService.getCurrentLocale());
	}
	
	public String getMessage(String messageCode, Object... arguments) {
		return messageSource.getMessage(messageCode, arguments, localeService.getCurrentLocale());
	}
	
	public String getMessage(String messageCode, String defaultMessage, Object... arguments) {
		return messageSource.getMessage(messageCode, arguments, defaultMessage, localeService.getCurrentLocale());
	}
	
	
	/* URLs */
	
	public URL getMessageAsUrl(String messageCode) {
		return AppConstants.class.getResource(messageSource.getMessage(messageCode, null, localeService.getCurrentLocale()));
	}
	
	public URL getMessageAsUrl(String messageCode, Locale locale) {
		return AppConstants.class.getResource(messageSource.getMessage(messageCode, null, locale));
	}
}
